package com.flame.shapeville.component.task3;

import javafx.concurrent.Worker;
import javafx.scene.control.Label;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.scene.shape.Polygon;
import javafx.scene.shape.Shape;
import javafx.scene.web.WebView;

/**
 * Represents a parallelogram used in Task 3.
 * <p>
 * This class draws a slanted parallelogram with customizable base, height, and angle,
 * and provides a detailed view with formula explanation.
 *
 * @author Flame Dev Team
 * @version 1.0
 * @since 2025-05-14
 */
public class JfParallelogram extends JfShape {

    /**
     * Size of the drawing area.
     */
    private int size;

    /**
     * Base length of the parallelogram.
     */
    private int base;

    /**
     * Height of the parallelogram.
     */
    private int height;

    /**
     * Angle of the parallelogram's slant (in degrees).
     */
    private int angle;

    /**
     * Constructs a new parallelogram with specified dimensions and angle.
     *
     * @param size the width/height of the drawing area
     * @param base the base length of the parallelogram
     * @param height the height of the parallelogram
     * @param angle the slant angle in degrees
     */
    public JfParallelogram(int size, int base, int height, int angle) {
        super();
        this.size = size;
        this.base = base;
        this.height = height;
        this.angle = angle;
        this.graphics = loadShape(size, centerX, centerY);
        shape.getChildren().add(graphics);
        initDetailed(size);
    }

    /**
     * Constructs a new parallelogram with default angle of 60°.
     *
     * @param size the width/height of the drawing area
     * @param base the base length of the parallelogram
     * @param height the height of the parallelogram
     */
    public JfParallelogram(int size, int base, int height) {
        this(size, base, height, 60);
    }

    /**
     * Loads the visual representation of the parallelogram into the provided pane.
     *
     * @param size the width/height of the drawing area
     * @param x X-coordinate center of the shape
     * @param y Y-coordinate center of the shape
     * @return the Shape object representing the parallelogram
     */
    private Shape loadShape(int size, double x, double y) {
        int multiple = Math.min(size / (base + (int) (height * Math.sin(Math.toRadians(angle)))), size / height);
        Shape shape = new Polygon(
                height * multiple * Math.sin(Math.toRadians(angle)), size - height * multiple,
                0, size,
                base * multiple, size,
                height * multiple * Math.sin(Math.toRadians(angle)) + base * multiple, size - height * multiple
        );
        initBaseParams(shape);
        shape.setLayoutX(x - (double) (base * multiple) / 2);
        shape.setLayoutY(- (height * multiple));
        return shape;
    }

    /**
     * Updates the angle and redraws the shape.
     *
     * @param angle the new slant angle in degrees
     */
    public void setAngle(int angle) {
        this.angle = angle;
        loadShape(size, centerX, centerY);
    }

    /**
     * Initializes the detailed view of the parallelogram.
     * Includes formula explanation using an embedded web view.
     *
     * @param graphicsSize the size used for rendering the detailed shape
     */
    @Override
    void initDetailed(int graphicsSize) {
        Label dataLabel = new Label("base = " + base + "\nheight = " + height);
        dataLabel.setLayoutX(50);
        dataLabel.setLayoutY(10);

        Shape detailedGraphics = loadShape(graphicsSize, centerX - this.shape.getPrefWidth() / 4, centerY);

        Line dashedLine = new Line();
        dashedLine.setStartX(0);
        dashedLine.setStartY(20);
        dashedLine.setEndX(0);
        dashedLine.setEndY(220);
        dashedLine.setStroke(Color.GRAY);
        dashedLine.setStrokeWidth(2);
        dashedLine.getStrokeDashArray().addAll(10d, 4d);
        dashedLine.setTranslateX(centerX - 1);

        WebView webView = new WebView();
        webView.setPrefSize(220, 240);
        webView.setLayoutX(centerX + 1);
        webView.setStyle("-fx-background-color: transparent;");
        String url = getClass().getResource("/static/formula/task3/parallelogram.html").toExternalForm();
        webView.getEngine().load(url);
        webView.getEngine().getLoadWorker().stateProperty().addListener((obs, oldState, newState) -> {
            if (newState == Worker.State.SUCCEEDED) {
                webView.getEngine().executeScript("updateFormula(" + base + "," + height + ")");
            }
        });

        detailedShape.getChildren().addAll(dataLabel, detailedGraphics, dashedLine, webView);
    }
}
